package week4;

/**
 *
 * 200. 岛屿数量
 * https://leetcode-cn.com/problems/number-of-islands/
 *
 *
 *
 *
 */
public class Week4NumberOfIslands {

    public int numIslands(char[][] grid) {
        int islandNum = 0;
        if(grid != null && grid.length > 0 && grid[0].length > 0) {
            int rowLength = grid.length;
            int columnLength = grid[0].length;

            for(int rowSize = 0; rowSize < rowLength; rowSize++) {
                // rowSize 每行
                for(int columnSize = 0; columnSize < columnLength; columnLength++) {
                    // columnSize rowSize行每列
                    if (grid[rowSize][columnSize] == '1') {
                        islandNum++;
                        System.out.println(" rowSize:" + rowSize + " columnSize:" + columnSize);
                        depthFirstSearch(grid, rowSize, columnSize);
                    }
                }

            }

        }

        return islandNum;
    }

    public void depthFirstSearch(char[][] grid, int rowSize, int columnSize) {
        System.out.println(" depthFirstSearch rowSize:" + rowSize + " columnSize:" + columnSize);
        int rowLength = grid.length;
        int columnLength = grid[0].length;

        if(rowSize < 0 || columnSize < 0 || rowSize >= rowLength || columnSize >= columnLength
                || grid[rowSize][columnSize] == '0' ) {
            return;
        }

        grid[rowSize][columnSize] = '0';
        depthFirstSearch(grid, rowSize - 1, columnSize);
        depthFirstSearch(grid, rowSize + 1, columnSize);
        depthFirstSearch(grid, rowSize, columnSize - 1);
        depthFirstSearch(grid, rowSize, columnSize + 1);

    }
}
